package com.dongdong.android.framework.imageloader;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.dongdong.android.framework.R;

/**
 * Created by dongdong on 2018/3/28.
 */

public class ImageViewEx extends AppCompatImageView {


    private int radius = 0;

    private boolean isCircle;

    private Drawable loadingDrawable;

    private Drawable errorDrawable;

    public ImageViewEx(Context context) {
        super(context);
    }

    public ImageViewEx(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImageViewEx(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray attrArray = context.obtainStyledAttributes(attrs, R.styleable.ImageViewEx);
        int count = attrArray.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attrName = attrArray.getIndex(i);
            if (attrName == R.styleable.ImageViewEx_circle) {
                isCircle = attrArray.getBoolean(i, false);
            } else if (attrName == R.styleable.ImageViewEx_radio) {
                radius = attrArray.getDimensionPixelSize(i, 0);
            } else if (attrName == R.styleable.ImageViewEx_placeHolder) {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    //noinspection deprecation
                    loadingDrawable = getResources().getDrawable(attrArray.getResourceId
                            (attrName, 0));
                } else {
                    loadingDrawable = getResources().getDrawable(attrArray.getResourceId
                            (attrName, 0), context.getTheme());
                }
            } else if (attrName == R.styleable.ImageViewEx_errorHolder) {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    //noinspection deprecation
                    errorDrawable = getResources().getDrawable(attrArray.getResourceId(attrName,
                            0));
                } else {
                    errorDrawable = getResources().getDrawable(attrArray.getResourceId(attrName,
                            0), context.getTheme());
                }
            }
        }
        attrArray.recycle();
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }


    public void setCircle(boolean circle) {
        isCircle = circle;
    }


    public void setImageUri(String uri) {

        this.setImageUri(uri, loadingDrawable, errorDrawable);

    }

    public void setImageUri(String uri, DiskCacheStrategy strategy) {

        this.setImageUri(uri, loadingDrawable, errorDrawable, strategy);

    }

    public void setImageUriWithSize(String uri, int width, int height) {

        Glide.with(this.getContext())
                .load(uri)
                .override(width, height)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .fitCenter().into(this);
    }

    public void setImageUri(String uri, int width, int height, Drawable loadingDrawable, Drawable
            errorDrawable) {

        if (isCircle) {
            Glide.with(this.getContext()).load(uri).placeholder(loadingDrawable).error
                    (errorDrawable).transform(new CircleTransform(this.getContext()))
                    .override(width, height)
                    .dontAnimate().into(this);
        } else {
            if (radius > 0) {
                Glide.with(this.getContext()).load(uri).placeholder(loadingDrawable).error
                        (errorDrawable).transform(new CornersTransform(this.getContext(), radius))
                        .override(width, height)
                        .diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate().into(this);
            } else {
                Glide.with(this.getContext()).load(uri).placeholder(loadingDrawable).error
                        (errorDrawable).diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate()
                        .override(width, height)
                        .into(this);
            }
        }
    }

    public void setImageUri(String uri, Drawable loadingDrawable, Drawable errorDrawable,
                            DiskCacheStrategy strategy) {

        if (isCircle) {
            Glide.with(this.getContext()).load(uri).placeholder(loadingDrawable).error
                    (errorDrawable).transform(new CircleTransform(this.getContext()))
                    .diskCacheStrategy(strategy).dontAnimate().into(this);
        } else {
            if (radius > 0) {
                Glide.with(this.getContext()).load(uri).placeholder(loadingDrawable).error
                        (errorDrawable).transform(new CornersTransform(this.getContext(), radius))
                        .diskCacheStrategy(strategy).dontAnimate().into(this);
            } else {
                Glide.with(this.getContext()).load(uri).placeholder(loadingDrawable).error
                        (errorDrawable).diskCacheStrategy(strategy).dontAnimate()
                        .into(this);
            }
        }
    }

    public void setImageUri(String uri, Drawable loadingDrawable, Drawable errorDrawable) {

        setImageUri(uri, loadingDrawable, errorDrawable, DiskCacheStrategy.ALL);
    }

    public void setImageUri(String uri, int errorDrawableRes) {

        this.setImageUri(uri, loadingDrawable, getResources()
                .getDrawable(errorDrawableRes));
    }

    public void setImageUri(String uri, int loadingDrawableRes, int errorDrawableRes) {

        this.setImageUri(uri, getResources().getDrawable(loadingDrawableRes), getResources()
                .getDrawable(errorDrawableRes));
    }

    public void setImageUri(String uri, int width, int height, int loadingDrawableRes, int
            errorDrawableRes) {

        this.setImageUri(uri, width, height, getResources().getDrawable(loadingDrawableRes),
                getResources()
                        .getDrawable(errorDrawableRes));
    }

    private SimpleTarget target = new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
            // do something with the bitmap
            // for demonstration purposes, let's just set it to an ImageView
            ImageViewEx.this.setImageBitmap(bitmap);
        }
    };

}


